#!perl unit test
use strict;
use warnings;
use lib 'lib';
use lib 't/mock';
use Test::More tests => 20;

use UserAgent qw(get prepare_testfile);
use Bunnies::QuoteCIBC qw(parse fetch);

my $testfile = prepare_testfile(
    {symbol=>"ASTOCK", last => 88.88, net => 0.88, bid=>88.18, ask=>88.98, volume_str=>"666,888"} 
);

my $ua = UserAgent->new(file=>$testfile);

my $qcibc = Bunnies::QuoteCIBC->new( ua=>$ua );
#original format
#<td nowrap><p><img src="images/up_arrow.gif">0.88</td>


my $html = `cat $testfile.ASTOCK`;

my $quote = $qcibc->parse($html);

cmp_ok($quote->{last},  'eq',88.88,     "last parsed ok");
cmp_ok($quote->{net},   'eq',0.88,      "net parsed ok");
cmp_ok($quote->{bid},   'eq',88.18,     "bid parsed ok");
cmp_ok($quote->{ask},   'eq',88.98,     "ask parsed ok");
cmp_ok($quote->{volume},'eq',666_888,   "volume parsed ok");

my $fetch_quote = $qcibc->fetch("TSE", "ASTOCK");

cmp_ok($fetch_quote->{last},  'eq',88.88,     "last parsed ok");
cmp_ok($fetch_quote->{net},   'eq',0.88,      "net parsed ok");
cmp_ok($fetch_quote->{bid},   'eq',88.18,     "bid parsed ok");
cmp_ok($fetch_quote->{ask},   'eq',88.98,     "ask parsed ok");
cmp_ok($fetch_quote->{volume},'eq',666_888,   "volume parsed ok");

$testfile = prepare_testfile(
    {symbol=>"ASTOCK", last => 8.58, net => -0.28, bid=>8.38, ask=>8.78, volume_str=>"1,999,888"}, 
);

$html = `cat $testfile.ASTOCK`;
$quote = $qcibc->parse($html);
$fetch_quote = $qcibc->fetch("US", "ASTOCK");

cmp_ok($quote->{last},  'eq',8.58,     "last parsed ok");
cmp_ok($quote->{net},   'eq',-0.28,    "minus net parsed ok");
cmp_ok($quote->{bid},   'eq',8.38,     "bid parsed ok");
cmp_ok($quote->{ask},   'eq',8.78,     "ask parsed ok");
cmp_ok($quote->{volume},'eq',1_999_888,   "volume parsed ok");

cmp_ok($fetch_quote->{last},  'eq',8.58,     "last parsed ok");
cmp_ok($fetch_quote->{net},   'eq',-0.28,    "minus net parsed ok");
cmp_ok($fetch_quote->{bid},   'eq',8.38,     "bid parsed ok");
cmp_ok($fetch_quote->{ask},   'eq',8.78,     "ask parsed ok");
cmp_ok($fetch_quote->{volume},'eq',1_999_888,   "volume parsed ok");


cleanup: {

system("rm $testfile.ASTOCK");

}
